package com.qd.panda.space.publish;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.constant.ConstantDto;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.sys.annotation.DataScope;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.common.panda.domain.entity.space.publish.ParkSpacePublishDTO;
import com.qd.common.panda.domain.entity.space.publish.ParkSpacePublishListDTO;
import com.qd.panda.service.space.publish.ParkSpacePublishHolderService;
import com.qd.panda.service.space.publish.ParkSpacePublishListService;
import com.qd.panda.service.space.publish.ParkSpacePublishService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 短期租赁/WEB_发布车位租赁
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/parkSpacePublish/")
@Slf4j
public class ParkSpacePublishController {
    private final ParkSpacePublishHolderService parkSpacePublishHolderService;
    private final ParkSpacePublishService parkSpacePublishService;
    private final ParkSpacePublishListService parkSpacePublishListService;

    /**
     * 发布车位
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:parkSpacePublish:save')")
    @OperatorInfo(title = "添加", operator = OperatorType.INSERT)
    @PostMapping(value = "/save")
    public ApiResultWrapper<Object> save(@Validated({DefaultGroup.class}) @RequestBody ParkSpacePublishDTO dto) {
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setPublishTime(LocalDateTime.now());
        dto.setEnable(ConstantDto.SF1);
        parkSpacePublishHolderService.save(dto);
        return ApiResult.success();
    }

    /**
     * 停用
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:parkSpacePublish:stop')")
    @OperatorInfo(title = "停用", operator = OperatorType.UPDATE)
    @PutMapping(value = "/stop")
    public ApiResultWrapper<Object> stop(@Validated @RequestBody ParkSpacePublishDTO dto) {
        if (!StringUtils.hasLength(dto.getPublishId())) {
            throw new ApiException("发布ID不能为空");
        }
        parkSpacePublishHolderService.stop(dto);
        return ApiResult.success();
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:parkSpacePublish:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        parkSpacePublishHolderService.removeBatchIds(ids);
        return ApiResult.success("删除成功");
    }

    /**
     * 发布详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ParkSpacePublishDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return parkSpacePublishService.getByIdDto(id);
    }

    /**
     * 发布列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @DataScope(orgAlias = "t1")
    @GetMapping(value = "/page")
    public IPage<ParkSpacePublishDTO> page(ParkSpacePublishDTO dto) {
        return parkSpacePublishService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 车位列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/spacePage")
    public IPage<ParkSpacePublishListDTO> spacePage(ParkSpacePublishListDTO dto) {
        return parkSpacePublishListService.page(PageUtil.buildPage(dto), dto);
    }
}